//
//  ResetPasswordController.m
//  International_Hospital
//
//  Created by 统杰李 on 16/7/8.
//  Copyright © 2016年 LiYiLin. All rights reserved.
//

#import "ResetPasswordController.h"

#import "CursorTextField.h"

@interface ResetPasswordController ()

//原始密码
@property (nonatomic, weak)CursorTextField *originalPassword;
//新密码
@property (nonatomic, weak)CursorTextField *newsPassword;
//确认新密码
@property (nonatomic, weak)CursorTextField *affirmPassword;
//确认button
@property (nonatomic, weak)UIButton *affirmBtn;

@end

@implementation ResetPasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置第一响应者
    [self.originalPassword becomeFirstResponder];
    
    [self setupUI];
    
}

#pragma mark
#pragma mark -  设置UI界面
- (void)setupUI {
    
    //创建
    self.originalPassword = [self creatTExtFieldWith:@"请输入原密码"];
    self.newsPassword = [self creatTExtFieldWith:@"请输入新密码"];
    self.affirmPassword = [self creatTExtFieldWith:@"再次输入新密码"];
    [self creatBtn];
    
    //约束
    [self.originalPassword makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).offset(150);
        make.left.equalTo(self.view.left).offset(30);
        make.right.equalTo(self.view.right).offset(-30);
        make.height.equalTo(40);
        
    }];
    
    [self.newsPassword makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.originalPassword.bottom).offset(30);
        make.left.equalTo(self.originalPassword.left);
        make.right.equalTo(self.originalPassword.right);
        make.height.equalTo(40);
        
    }];
    
    [self.affirmPassword makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.newsPassword.bottom).offset(30);
        make.left.equalTo(self.originalPassword.left);
        make.right.equalTo(self.originalPassword.right);
        make.height.equalTo(40);
        
    }];
    
    [self.affirmBtn makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.affirmPassword.bottom).offset(40);
        make.left.equalTo(self.view.left).offset(100);
        make.right.equalTo(self.view.right).offset(-100);
        make.height.equalTo(40);
        
    }];
    
    
}

#pragma mark
#pragma mark -  创建textField的方法
-(CursorTextField *)creatTExtFieldWith:(NSString *)placeeHolder{
    
    CursorTextField *textField = [[CursorTextField alloc] init];
    // 将图层的边框设置为圆脚
    textField.layer.cornerRadius = 8;
    // 给图层添加一个有色边框
    textField.layer.borderWidth = 1.5;
    
    textField.layer.borderColor = [UIColor colorWithRed:0.3 green:0.88 blue:0.88 alpha:1].CGColor;
    
    textField.placeholder = placeeHolder;
    
    [self.view addSubview:textField];
    
    return textField;
    
}

#pragma mark
#pragma mark - 创建btn
- (void)creatBtn{
    
    UIButton *btn = [[UIButton alloc] init];
    
    self.affirmBtn = btn;
    
    [btn setBackgroundColor:[UIColor colorWithRed:0.3 green:0.88 blue:0.88 alpha:1]];
    
    [btn setTitle:@"确认" forState:UIControlStateNormal];
    
    // 将图层的边框设置为圆脚
    btn.layer.cornerRadius = 8;
    
    [btn addTarget:self action:@selector(didClickBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btn];
    
}

#pragma mark
#pragma mark - 点击btn
- (void)didClickBtn{
    
    if (![self.originalPassword.text isEqualToString:@"aa"]) {
        
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD setMinimumDismissTimeInterval:1.5];
        [SVProgressHUD showErrorWithStatus:@"原密码输入错误,请重新输入"];
        [self cleartextField];
        
    }else if([self.originalPassword.text isEqualToString:@"aa"] && [self.newsPassword.text isEqualToString:self.affirmPassword.text]) {
        
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD setMinimumDismissTimeInterval:1.5];
        [SVProgressHUD showSuccessWithStatus:@"修改成功"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self.navigationController popViewControllerAnimated:YES];
            
        });
        
    }else if(![self.newsPassword.text isEqualToString:self.affirmPassword.text]){
        
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD setMinimumDismissTimeInterval:1.5];
        [SVProgressHUD showErrorWithStatus:@"两次密码输入不同,请重新输入"];
        [self cleartextField];
        
    }
    
}

#pragma mark
#pragma mark - 清空textField的方法
- (void)cleartextField{
    
    [self.originalPassword setText:@""];
    [self.newsPassword setText:@""];
    [self.affirmPassword setText:@""];
    //设置第一响应者
    [self.originalPassword becomeFirstResponder];
    
}
@end
